package GitObjController

import (
	"arabApi/app/api/controllers"
	"arabApi/app/api/controllers/CFun"
	"arabApi/app/api/requests"
	"arabApi/app/api/requests/CRequest"
	"arabApi/app/api/requests/ObjGitRequest"
	"arabApi/app/api/services/ObjGitService"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type PushPath struct {
	controllers.BaseAPIController
}

func (r PushPath) List(c *gin.Context) {
	id := ObjGitRequest.ObjGitPushPathJobId{}
	if err := c.ShouldBindQuery(&id); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &id, ObjGitRequest.CheckObjGitPushPathJobId); !ok {
		return
	}

	data := ObjGitService.PathList(id.JobId)
	CFun.Ajax_return_success(c, CFun.WithData(data))

}

func (r PushPath) MsgById(c *gin.Context) {
	id := CRequest.Oid{}
	if err := c.ShouldBindQuery(&id); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &id, CRequest.CheckID); !ok {
		return
	}

	msg := ObjGitService.PathMsgByID(id.Id)
	CFun.Ajax_return_success(c, CFun.WithData(msg))
}

func (r PushPath) Edit(c *gin.Context) {

	s := ObjGitRequest.ObjGitPushPath{}
	if err := c.ShouldBindBodyWith(&s, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &s, ObjGitRequest.CheckObjGitPushPath); !ok {
		return
	}

	if err := ObjGitService.PathEdit(s); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("编辑失败"+err.Error()))
	} else {
		CFun.Ajax_return_success(c)
	}

}

func (r PushPath) Add(c *gin.Context) {

	s := ObjGitRequest.ObjGitPushPath{}
	if err := c.ShouldBindBodyWith(&s, binding.JSON); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &s, ObjGitRequest.CheckObjGitPushPath); !ok {
		return
	}

	if id, err := ObjGitService.PathAdd(s); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("添加失败"+err.Error()))
	} else {
		CFun.Ajax_return_success(c, CFun.WithData(id), CFun.WithMsg("添加成功"))
	}
}

func (r PushPath) Group(c *gin.Context) {

	id := CRequest.Oid{}
	if err := c.ShouldBindQuery(&id); err != nil {
		CFun.Ajax_return_fail(c, CFun.WithMsg("未找到对应的ID"+err.Error()))
		return
	}
	if ok := requests.Validate(c, &id, CRequest.CheckID); !ok {
		return
	}
	job := ObjGitService.BranchMsgByID(id.Id)

	data := gin.H{}
	data["test"] = ObjGitService.Group(job.ObId, 2)
	data["yufabu"] = ObjGitService.Group(job.ObId, 3)
	data["zhengshi"] = ObjGitService.Group(job.ObId, 4)
	CFun.Ajax_return_success(c, CFun.WithData(data))

}
